package cn.cl.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.cl.gulimall.ware.vo.MergeVo;
import cn.cl.gulimall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.cl.gulimall.ware.entity.PurchaseEntity;
import cn.cl.gulimall.ware.service.PurchaseService;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.R;


/**
 * 采购信息
 *
 * @author 高志红
 * @email 1739745826@qq.com
 * @date 2020-05-08 07:47:36
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
	@Autowired
	private PurchaseService purchaseService;


	/**
	 * 列表
	 */
	@RequestMapping("/list")
	// @RequiresPermissions("ware:purchase:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = purchaseService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	// @RequiresPermissions("ware:purchase:info")
	public R info(@PathVariable("id") Long id) {
		PurchaseEntity purchase = purchaseService.getById(id);

		return R.ok().put("purchase", purchase);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody PurchaseEntity purchase) {
		purchase.setCreateTime(new Date());
		purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody PurchaseEntity purchase) {
		purchaseService.updateById(purchase);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] ids) {
		purchaseService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	/**
	 * 未完成的采购需求
	 */
	@RequestMapping("/unreceive/list")
	public R unreceiveList(@RequestParam Map<String, Object> params) {
		PageUtils page = purchaseService.queryPageUnreceive(params);
		return R.ok().put("page", page);
	}

	/**
	 * 合并采购细求
	 */
	@PostMapping("/merge")
	public R merge(@RequestBody MergeVo mergeVo) {
		purchaseService.mergePurchase(mergeVo);
		return R.ok();
	}

	/**
	 * 领取采购单
	 */
	@PostMapping("/received")
	public R received(@RequestBody List<Long> ids) {
		purchaseService.received(ids);
		return R.ok();
	}

	/**
	 * 完成采购单
	 */
	@PostMapping("/done")
	public R done(@RequestBody PurchaseDoneVo purchaseDoneVo) {
		purchaseService.done(purchaseDoneVo);
		return R.ok();
	}
}
